package org.dentalcard.service.impl;

import org.dentalcard.entity.DecUser;
import org.dentalcard.entity.Shift;
import org.dentalcard.entity.Visit;
import org.dentalcard.entity.WorkDay;
import org.dentalcard.service.PersistenceManager;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 9:19
 */
public class ScheduleDaoImpl {

    public List<Shift> getShiftList(DecUser user){
        Session session = PersistenceManager.getSession();
        try {
            Criteria criteria = session.createCriteria(Shift.class);
            criteria.addOrder(Order.asc("nByOrder"));
            return (List<Shift>) criteria.list();
        } finally {
            session.close();
        }
    }

    
    public List<WorkDay> getWorkDayList(DecUser user){
        Session session = PersistenceManager.getSession();
        try {
            Criteria criteria = session.createCriteria(WorkDay.class);
            criteria.add(Restrictions.eq("userId", user.getId()));
            criteria.addOrder(Order.asc("day"));
            return (List<WorkDay>) criteria.list();
        } finally {
            session.close();
        }
    }

    public WorkDay getWorkDay(DecUser user, Date date){
        Session session = PersistenceManager.getSession();
        try {
            Criteria criteria = session.createCriteria(WorkDay.class);
            criteria.add(Restrictions.and(Restrictions.eq("userId", user.getId()),
                    Restrictions.eq("day", date)));
            return (WorkDay) criteria.uniqueResult();
        } finally {
            session.close();
        }
    }

    public WorkDay getShift(DecUser user, Date date) {
        Session session = PersistenceManager.getSession();
        try {
            Criteria criteria = session.createCriteria(WorkDay.class);
            criteria.add(Restrictions.eq("day", date));
            return (WorkDay) criteria.uniqueResult();
        } finally {
            session.close();
        }
    }
    
    public List<Visit> getVisits(WorkDay workDay){
        Session session = PersistenceManager.getSession();
        try {
            Criteria criteria = session.createCriteria(Visit.class);
            criteria.add(Restrictions.and(Restrictions.eq("workDayDay", workDay.getDay()),
                    Restrictions.eq("workDayUserId", workDay.getUserId())));
            return (List<Visit>) criteria.list();
        } finally {
            session.close();
        }

    }
    
}
